import { post, get } from "../../../utils/request";
import utils from "../../../utils/utils";
const env = import.meta.env;

const apiAccount = {
  queryAccountListing(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.listing",
      data
    );
  },

  getCurrentAccount() {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.current.get",
      {}
    );
  },
  getAccount(accountId: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.get", {
      accountId: accountId,
    });
  },
  getAccountExtension(accountId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.extension.get",
      {
        accountId: accountId,
      }
    );
  },
  queryAccountRole(accountId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.role.all",
      {
        accountId: accountId,
      }
    );
  },
  updateAccount(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.update",
      data
    );
  },
  deleteAccount(accountId: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.delete", {
      accountId,
    });
  },
  changeAccountStatus(accountId: string, status: number) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.status.change",
      {
        accountId,
        status,
      }
    );
  },
  // 服务器端返回加密用的公钥
  generateSessionPublicKeyQ(sessionId: string) {
    return get(env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/public.key.get", {
      sessionId,
    });
  },
  changeAccountPassword(
    sessionId: string,
    accountId: string,
    encryptedPassword: string
  ) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.password",
      {
        sessionId,
        accountId,
        encryptedPassword,
      }
    );
  },

  queryAccountLogListing(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.log.listing",
      data
    );
  },
  getAccountLog(accountLogId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.log.get",
      {
        accountLogId: accountLogId,
      }
    );
  },
  getAccountPrevLog(accountLogId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.log.prev.get",
      {
        accountLogId: accountLogId,
      }
    );
  },
  getAccountNextLog(accountLogId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.log.next.get",
      {
        accountLogId: accountLogId,
      }
    );
  },
  /**
   * 查询匹配的账号，最多返回10条记录
   * @param name 可以是账号、姓名
   * @param roleCode 角色代码，可以输入空字符串
   * @param orgCode 限定所在单位，可以输入空字符串
   * @returns
   */
  queryTop10Account(name: string, roleCode: string, orgCode: string) {
    return post(
      env.VITE_API_AUTH_CONTEXT_PATH + "/admin/api/user.by.name.top10.listing",
      {
        name: name,
        roleCode,
        orgCode,
      }
    );
  },
  queryTop10AccountAsync(
    queryString: string,
    roleCode: string,
    orgCode: string,
    cb: Function
  ) {
    // 处理业务逻辑，触发关闭事件
    if (queryString.length < 1) {
      cb([]);
      return;
    }

    apiAccount
      .queryTop10Account(queryString, roleCode, orgCode)
      .then((response: any) => {
        if (response.data.code == 200) {
          cb(response.data.data);
        }
      })
      .catch((err: any) => {
        utils.errorNotification("数据处理错误", err);
      });
  },
  queryAccountSignupListing(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/signup.listing",
      data
    );
  },
  getAccountSignup(accountSignupId: string) {
    return get(
      env.VITE_API_ADMIN_CONTEXT_PATH +
      "/admin/api/signup?accountSignupId=" +
      accountSignupId,
      null
    );
  },
  changeSignupStatus(
    accountSignupIds: string,
    status: number,
    description: string
  ) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/signup.status", {
      accountSignupIds,
      status,
      description,
    });
  },
  /**
   * 20230720通过角色代码查询账号
   * @returns 
   */
  queryAccountByRoleCode(roleCode: string,) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/account.query.rolecode", {
      roleCode
    }
    );
  },
};

export default apiAccount;
